﻿<template>
    <full-screen-dialog
        title="设置机构"
        v-model="visible"
        :width="1300"
        destroy-on-close
        :close-on-click-modal="false"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="110px" label="账号">{{ entityObj.accountCode }}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="110px" label="姓名">{{ entityObj.realName}}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item
                        label-width="110px"
                        label="手机号"
                    >{{ entityObj.mobile }}</el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="110px" label="绑定机构" prop="applyOrderItems">
                        <el-button size="small" @click="addPurchaseUnity()">绑定供货单位</el-button>
                        <el-button size="small" @click="addStore()">绑定门店</el-button>
                        <el-button size="small" @click="addSupplier()">绑定供应商</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="110px">
                        <el-table :data="entityObj.orgs" border style="width: 100%" show-summary>
                            <el-table-column label="机构类型" min-width="110">
                                <template #default="scope">
                                    <span v-if="scope.row.orgType==1">供货单位</span>
                                    <span v-else-if="scope.row.orgType==2">门店</span>
                                    <span v-else-if="scope.row.orgType==3">供应商</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="机构名称" min-width="260">
                                <template #default="scope">{{scope.row.orgName}}</template>
                            </el-table-column>
                            <el-table-column label="操作" width="80">
                                <template #default="scope">
                                    <el-button
                                        size="small"
                                        @click="deleteRow(scope.$index, scope.row)"
                                    >移除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>

        <template>
            <full-screen-dialog
                v-model="supplierState"
                title="选择供应商"
                :width="1300"
                :center="true"
                append-to-body
                style="height: 480px"
            >
                <el-row>
                    <sc-supplier-choice
                        ref="scSupplierChoice"
                        :defaultQueryList="supplierQueryList"
                    ></sc-supplier-choice>
                </el-row>
                <template #footer>
                    <el-button @click="supplierState = false">取 消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="setSupplierChoice">确定</el-button>
                </template>
            </full-screen-dialog>
        </template>

        <full-screen-dialog
            v-model="storeState"
            title="选择门店"
            :width="1300"
            :center="true"
            append-to-body
            style="height: 480px"
        >
            <el-row>
                <sc-store-choice ref="scStoreChoice" :defaultQueryList="storeQueryList"></sc-store-choice>
            </el-row>
            <template #footer>
                <el-button @click="storeState = false">取 消</el-button>
                <el-button type="primary" :loading="isSaveing" @click="setStoreChoice">确定</el-button>
            </template>
        </full-screen-dialog>

        <template>
            <full-screen-dialog
                v-model="purchaseUnityState"
                title="选择供货单位"
                :width="1300"
                :center="true"
                append-to-body
                style="height: 480px"
            >
                <el-row>
                    <sc-purchase-unity-choice
                        ref="scPurchaseUnityChoice"
                        :defaultQueryList="purchaseUnityQueryList"
                    ></sc-purchase-unity-choice>
                </el-row>
                <template #footer>
                    <el-button @click="purchaseUnityState = false">取 消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="setPurchaseUnityChoice">确定</el-button>
                </template>
            </full-screen-dialog>
        </template>
    </full-screen-dialog>
</template>



<script>
import tool from "@/utils/tool";
import { filter } from 'jszip';
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "设置配送供应商",
                Show: "查看",
            },
            entityObj: {
                supplierIds: [],
                suppliers: [],
            },
            applyOrderState: false,
            queryList: {},
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            //验证规则
            rules: {},
            props: {
                label: "supplierName",
                value: "supplierId",
            },
            materialProps: {
                label: "materialCode",
                value: "materialId",
                items: "materialItems",
            },
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            optionsPurchaseUnity: [],
            itemsData: [],
            supplierState:false,
            supplierQueryList:{},
            storeState:false,
            storeQueryList:{},
            purchaseUnityState:false,
            purchaseUnityQueryList:{},
        };
    },
    computed: {},
    methods: {
        //显示=
        async open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "account/set-orgs",
                                this.entityObj
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            console.log("data", data);
            const returnValue = await this.$API.auth.GetProcessApi.get(
                `account/auth-org/${data.accountId}`,
                {}
            );
            if (returnValue.status == 200) {
                this.entityObj = { ...data, orgs: returnValue.response };
                console.log(this.entityObj);
            }
        },
        //删除
        deleteRow(index) {
            this.entityObj.orgs.splice(index, 1);
        },
        //获取供应商
        addSupplier() {
            const supplierIds = this.entityObj.orgs.filter(t=>t.orgType==3).map((item) => item.orgId);
            this.supplierQueryList = {
                //isAll: true,
                notSupplierIds: supplierIds,
            };
            this.supplierState = true;
            this.$nextTick(() => {
                this.$refs.scSupplierChoice.opBindData();
            });
        },
        //设置供应商
        setSupplierChoice() {
            let suppliers = this.$refs.scSupplierChoice.selectionRows;
            suppliers.map((p) => {
                if (!this.entityObj.orgs.some((t) =>t.orgType==3&& t.orgId == p.supplierId)) {
                    this.entityObj.orgs.push({
                        orgType: 3,
                        orgId: p.supplierId,
                        orgName: p.supplierName,
                    });
                }
            });
            this.supplierState = false;
        },
        //获取门店
        addStore() {
            const storeIds = this.entityObj.orgs.filter(t=>t.orgType==2).map((item) => item.orgId);
            this.storeQueryList = {
                //isAll: true,
                notStoreIds: storeIds,
            };
            this.storeState = true;
            this.$nextTick(() => {
                this.$refs.scStoreChoice.opBindData();
            });
        },
        //设置门店
        setStoreChoice() {
            let stores = this.$refs.scStoreChoice.selectionRows;
            stores.map((p) => {
                if (!this.entityObj.orgs.some((t) =>t.orgType==2&& t.orgId == p.storeId)) {
                    this.entityObj.orgs.push({
                        orgType: 2,
                        orgId: p.storeId,
                        orgName: p.storeName,
                    });
                }
            });
            this.storeState = false;
        },
        //获取供货单位
        addPurchaseUnity() {
            const purchaseUnityIds = this.entityObj.orgs.filter(t=>t.orgType==1).map((item) => item.orgId);
            this.purchaseUnityQueryList = {
                isAll: true,
                notPurchaseUnityIds: purchaseUnityIds,
            };
            this.purchaseUnityState = true;
            this.$nextTick(() => {
                this.$refs.scPurchaseUnityChoice.opBindData();
            });
        },
        //设置供货单位
        setPurchaseUnityChoice() {
            let purchaseUnitys = this.$refs.scPurchaseUnityChoice.selectionRows;
            purchaseUnitys.map((p) => {
                if (!this.entityObj.orgs.some((t) =>t.orgType==1&& t.orgId == p.purchaseUnityId)) {
                    this.entityObj.orgs.push({
                        orgType: 1,
                        orgId: p.purchaseUnityId,
                        orgName: p.purchaseUnityName,
                    });
                }
            });
            this.purchaseUnityState = false;
        },
    },
    async mounted() {},
};
</script>

<style></style>
